﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AliSMS
{
    public partial class FormTemplateInfo : Form
    {
        public FormTemplateInfo()
        {
            InitializeComponent();
        }

        InitFile init = new InitFile();
        SQLiteHelper sqlite = new SQLiteHelper();
        private void ExpressInfo_Load(object sender, EventArgs e)
        {
            ShowList();
        }
        public void ShowList()
        {
            try
            {
                dGVEntList.DataSource = null;
                string con = tbSearch.Text;
                string sql = "select id,TempLateTitle,TempLateCode,Beactive from [TempLate] ";
                if (!string.IsNullOrEmpty(con))
                {
                    sql += " where (TempLateTitle like '%" + con + "%' or TempLateCode like '%" + con + "%')";
                }
                sql += " order by id";
                txtDebug.Text = sql;
                DataTable dt = sqlite.ExecuteDt(sql);
                dGVEntList.DataSource = dt;
                init.DataViewColumn(dGVEntList,"");
                init.AutoSizeColumn(dGVEntList);
            }
            catch (Exception ex)
            {
                init.WriteLog(ex.ToString(), true);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            ShowList();
        }

        private void tbSearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                dGVEntList.AutoGenerateColumns = false;
                string con = tbSearch.Text;
                DataSet ds = sqlite.ExecuteDs("select id,TempLateTitle,TempLateCode,Beactive from [TempLate] where (TempLateTitle like '%" + con + "%' or TempLateCode like '%" + con + "%') order by id");
                ds.Tables[0].DefaultView.Sort = "id";
                dGVEntList.DataSource = ds.Tables[0];
                init.DataViewColumn(dGVEntList, "");
                init.AutoSizeColumn(dGVEntList);
            }
            catch (Exception ex)
            {
                init.WriteLog(ex.ToString(), true);
            }
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormTempLateEdit Edit = new FormTempLateEdit();
            DialogResult dialogResult = Edit.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                ShowList();
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormTempLateEdit Edit = new FormTempLateEdit();
            if (dGVEntList.SelectedRows.Count > 0)
            {
                Edit.eid = Convert.ToInt16(dGVEntList.SelectedRows[0].Cells["id"].Value);
                DialogResult dialogResult = Edit.ShowDialog();
                if (dialogResult == DialogResult.OK)
                {
                    ShowList();
                }
            }
            else
            {
                MessageBox.Show("请先选中数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dGVEntList.SelectedRows.Count > 0)
            {
                string TempLateTitle = dGVEntList.SelectedRows[0].Cells["TempLateTitle"].Value.ToString();
                string eid = dGVEntList.SelectedRows[0].Cells["id"].Value.ToString();
                DialogResult dialogResult = MessageBox.Show("确定要删除【" + TempLateTitle + "】的信息？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.OK)
                {
                    string sql = "delete from [TempLate] where id=" + eid;
                    if (sqlite.ExecuteSql(sql) > 0)
                    {
                        MessageBox.Show("删除【" + TempLateTitle + "】 成功!","成功",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        ShowList();
                    }
                    else
                    {
                        MessageBox.Show("删除失败！","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    }
                }

            }
            else
            {
                MessageBox.Show("请先选中数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void dGVEntList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            using (SolidBrush brush = new SolidBrush(dGVEntList.RowHeadersDefaultCellStyle.ForeColor))
            {
                string s = (e.RowIndex + 1).ToString();
                e.Graphics.DrawString(s, e.InheritedRowStyle.Font, brush, e.RowBounds.Location.X, e.RowBounds.Location.Y + 5);
            }
        }

        private void dGVEntList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            txtCount.Text = dGVEntList.Rows.Count.ToString();
        }
    }
}
